package crm.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import crm.dbcoon.DBConnect;
import crm.model.Saletarget;
import crm.util.Pagination;

public class SaletargetControl {
	public static Pagination pageInstance = null;
	
	//获取一条动态
    public Saletarget getOneSaletargetById(int id){
 	   DBConnect dbc = new DBConnect();
 	   String sql = "select * from sale_target where id = "+id;
 	  Saletarget saletarget = new Saletarget();
 	   dbc.prepareStatement(sql);
 	   try{
 		   ResultSet rs = dbc.executeQuery();
 		   while(rs.next()){
 			  saletarget.setId(id);
 			  saletarget.setContent(rs.getString("content"));
 			  saletarget.setPerson_id(rs.getInt("person_id"));
 			  saletarget.setType(rs.getString("type"));
 			      
 		   }
 	   }
 		   catch(SQLException e){
 			   e.printStackTrace();
 		   }
 		   finally{
 			   dbc.close();
 		   }
 		   return saletarget;
 	   }
    
	// 增加销售目标
	public boolean insertSaletarget(Saletarget saletarget) {
		DBConnect dbc = new DBConnect();
		String sql = "insert into sale_target(content,person_id,type)values(?,?,?)";
		dbc.prepareStatement(sql);
		try {
			// dbc.setInt(1,thought.getId());
			dbc.setString(1, saletarget.getContent());
			dbc.setInt(2, saletarget.getPerson_id());
			dbc.setString(3,saletarget.getType());
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}

	// 根据id删除销售目标
	public boolean deleteSaletarget(int id) {
		DBConnect dbc = new DBConnect();
		String sql = "delete from sale_target where id=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}

	// 根据id修改销售目标
	public boolean updateSaletarget(Saletarget saletarget, int id) {
		DBConnect dbc = new DBConnect();
		String sql = "update sale_target set content='" + saletarget.getContent()
				+ "',person_id='" + saletarget.getPerson_id() + "',type='"
				+ saletarget.getType() + "'where id=" + id;
		dbc.prepareStatement(sql);
		try {
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}
	
	// 分页显示
	public Collection GetSaletarget(int page, int rowsPerPage, String addtime)
			throws SQLException, Exception {
		String sql = "SELECT * FROM sale_target order by " + addtime + " desc";
		pageInstance = new SaletargetControlPage(sql, rowsPerPage);
		return pageInstance.getPage(page);
	}

	class SaletargetControlPage extends Pagination {
		public SaletargetControlPage(String sql, int rowsPerPage) {
			super(sql, rowsPerPage, 1);
		}

		protected Collection<Saletarget> packResultSet(ResultSet rs)
				throws SQLException {
			Collection<Saletarget> SaletargetList = new ArrayList<Saletarget>();
			while (rs.next()) {
				Saletarget s = new Saletarget();
				s.setId(rs.getInt("id"));
				s.setContent(rs.getString("content"));
				s.setPerson_id(rs.getInt("person_id"));
				s.setType(rs.getString("type"));
				SaletargetList.add(s);
				s = null;
			}
			return SaletargetList;
		}
	}

}
